package main

import (
	"encoding/json"
	"fmt"
	"time"
)

// 细说Golang的JSON解析
func main() {
	type Fruit struct {
		Name  string
		Price string
	}
	type FruitBasket struct {
		Name    string
		Fruit   Fruit
		Id      int64 `json:"ref"`
		Created time.Time
	}

	// 由于json.UnMarshal()方法接收的是字节切片，所以首先需要把JSON字符串转换成字节切片c := []byte(s)
	jsonData := []byte(`{
			"Name":"Standard",
			"Fruit":{
				"Name":"Apple",
				"Price":"$1"
			},
			"ref":999,
			"Created":"2020-10-10T23:00:00Z"
		}`)
	var basket FruitBasket
	err := json.Unmarshal(jsonData, &basket)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(basket.Name, basket.Fruit, basket.Id, basket.Created)
	}

}
